#include "Log.h"
#include <sstream>
#include <iostream>
#include <time.h>

bool Log::_initialised;
Log Log::_instance;

Log::Log()
{
	_initialised = true;
}

Log::~Log()
{
	_initialised = false;
}

Log* Log::getInstance()
{
	return _initialised ? &_instance : 0;
}

void Log::info(const std::string& msg)
{
	std::cout << getCurrentTime() << " INFO: " << msg << std::endl;
}

void Log::warning(const std::string& msg)
{
	std::cout << getCurrentTime() << " WARNING: " << msg << std::endl;
}

void Log::error(const std::string& msg)
{
	std::cout << getCurrentTime() << " ERROR: " << msg << std::endl;
}

void Log::message(const std::string& level, const std::string& msg)
{
	std::cout << getCurrentTime() << " " << level << ": " << msg << std::endl;
}

std::string Log::getCurrentTime()
{
	time_t currentUnformattedTime;
	time(&currentUnformattedTime);
	struct tm* currentGMTTime = gmtime(&currentUnformattedTime);
	std::string formattedTime;
	std::stringstream ss;
	ss << currentGMTTime->tm_hour;
	formattedTime.append(ss.str()).append(":");
	ss.str("");
	ss << currentGMTTime->tm_min;
	formattedTime.append(ss.str()).append(":");
	ss.str("");
	ss << currentGMTTime->tm_sec;
	formattedTime.append(ss.str());
	
	return formattedTime;
}

std::string Log::intToString(int num)
{
	std::stringstream sstr;
	sstr << num;
	std::string str = sstr.str();
	return str;
}
